package com.itheima.heimademo01.controller;

import com.itheima.heimademo01.base.response.Result;
import com.itheima.heimademo01.pojo.dto.ArticleUpdateDTO;
import com.itheima.heimademo01.pojo.dto.PageBean;
import com.itheima.heimademo01.pojo.entity.Article;
import com.itheima.heimademo01.service.ArticleService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;


    /**
     * 新增文章
     *
     * @param article
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }


    /**
     * 文章列表(条件分页)
     *
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param state
     * @return
     */
    @GetMapping
    public Result<PageBean<Article>> list(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(required = false) Integer categoryId, @RequestParam(required = false) String state) {
        PageBean<Article> pb = articleService.list(pageNum, pageSize, categoryId, state);
        return Result.success(pb);
    }

    /**
     * 获取文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("detail")
    public Result<Article> detail(@RequestParam Integer id) {
        Article article = articleService.detail(id);
        return Result.success(article);
    }


    /**
     * 更新文章
     *
     * @param articleUpdateDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody ArticleUpdateDTO articleUpdateDTO) {
        articleService.update(articleUpdateDTO);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        articleService.delete(id);
        return Result.success();
    }
}
